# Copyright (c) 2025 Li Auto Inc. and its affiliates
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#
# binary-ghs.mk
#

define transform-o-to-executable
@mkdir -p $(dir $@)
@echo "LD	$@"
$(hide)rm -rf $(dir $@)t
$(hide)mkdir -p $(dir $@)t
$(hide)$(AR) -archive -o $(dir $@)t/libmvbs_app.a $(PRIVATE_ALL_OBJS)
$(hide)cp -a $(PRIVATE_LIBRARY) $(dir $@)t
$(hide)cd $(dir $@) &&										\
       $(LD) $(filter-out -l%, $(PRIVATE_LFLAGS))						\
             $(if $(strip $(PRIVATE_LDS)), $(GHS_OPT_PROGRAM_ENTRY) -T $(abspath $(PRIVATE_LDS)),)	\
             $(filter -l%, $(PRIVATE_LFLAGS))							\
             --whole-archive									\
             $(addprefix t/, $(notdir $(PRIVATE_LIBRARY)))					\
             t/libmvbs_app.a									\
             --no-whole-archive									\
             -o $(notdir $@) -map=$(notdir $@).map -hex=$(GHS_GLOBAL_HEX_NAME).hex
$(transform-o-to-executable-post)
$(hide)rm -rf $(dir $@)t
endef

define transform-o-to-static-lib
@mkdir -p $(dir $@)
@echo "AR	$@"
$(hide)$(AR) -archive -o $@ $(PRIVATE_ALL_OBJS)
endef

define transform-c-to-o
@mkdir -p $(dir $@)
@echo "CC	$<"
$(hide)$(CC) $(c-includes) $(PRIVATE_CFLAGS)	\
             -MD -MF $(patsubst %.o,%.d,$@) -c $< -o $@
endef

define transform-c-to-cmd
@echo "$@:$<" > $(patsubst %.o,%.cmd,$@)
@echo "$(CC) $(c-includes) $(PRIVATE_CFLAGS) -c $< -o $@" | tr '\n' ' ' | sed 's/\s\+/ /g' >> $(patsubst %.o,%.cmd,$@)
endef

# vim:ft=make
#
